package com.bc.bcapi.service;

import com.bc.bccore.redis.RedisService;
import com.bc.bcmodel.consts.RedissCacheKey;
import com.bc.bcmodel.req.QuartzHttpModel;
import com.bc.commoncore.util.JsonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;

import com.bc.bcapi.dao.T_Mng_quartzserviceMapper;
import com.bc.bcmodel.domain.T_Mng_quartzserviceModel;

/**
 * @Name T_Mng_quartzserviceService * @Description * @Author generation * @Date 2018/8/17 10:23:55 * @Version 1.0
 **/
@Service
public class T_Mng_quartzserviceService {
    @Resource
    T_Mng_quartzserviceMapper t_Mng_quartzserviceMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 保存单条数据     *      * @return
     */
   
    public int insert(T_Mng_quartzserviceModel req) {
        return t_Mng_quartzserviceMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    
    public int insertBatch(List<T_Mng_quartzserviceModel> req) {
        return t_Mng_quartzserviceMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    
    public T_Mng_quartzserviceModel findByNamedParam(T_Mng_quartzserviceModel req) {
        return t_Mng_quartzserviceMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    
    public List<T_Mng_quartzserviceModel> getAllList(T_Mng_quartzserviceModel req) {
        return t_Mng_quartzserviceMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    
    public PageInfo<T_Mng_quartzserviceModel> getList(T_Mng_quartzserviceModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Mng_quartzserviceModel> list = t_Mng_quartzserviceMapper.findByNamedParamList(req);
        return new PageInfo<T_Mng_quartzserviceModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    
    public int update(T_Mng_quartzserviceModel req) {
        return t_Mng_quartzserviceMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    
    public int delete(BigInteger id) {
        return t_Mng_quartzserviceMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    
    public long count(T_Mng_quartzserviceModel req) {
        return t_Mng_quartzserviceMapper.count(req);
    }

    public QuartzHttpModel queryStartList(int platform_id) {
        String redisKey = RedissCacheKey.QuartzConfig_Key + platform_id;
        String redisString = redisService.get(redisKey);
        if (redisString == null || redisString.isEmpty()) {
            QuartzHttpModel redisModel = new QuartzHttpModel();
            T_Mng_quartzserviceModel requestModel = new T_Mng_quartzserviceModel();
            requestModel.setIs_start(1);
            if (platform_id > 0) {
                requestModel.setPlatform_id(platform_id);
            }
            List<T_Mng_quartzserviceModel> listData = t_Mng_quartzserviceMapper.findByNamedParamList(requestModel);
            redisModel.setListData(listData);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String versionCode = sdf.format(new Date());
            redisModel.setVersionCode(versionCode);
            redisService.set(redisKey, JsonUtil.obj2string(redisModel), 360, TimeUnit.DAYS);
            return redisModel;
        }
        return JsonUtil.str2obj(redisString, QuartzHttpModel.class);
    }
}